package com.mxl.service.cashorder.rule;

import java.util.List;

import javax.annotation.Resource;

import com.mxl.common.BossTypeConverUtil;
import com.mxl.common.SelectOptionUtil;
import com.mxl.common.helper.ResponseErrorCode;
import com.mxl.common.rule.AbstractRule;
import com.mxl.dao.cashorder.CustomMerchantDao;
import com.mxl.dao.cashorder.model.CustomMerchant;
import com.mxl.service.cashorder.dto.CashOrderRequest;

/**
 * 密钥以后抽出来放到member表中,或者接口增加bosstype字段
 * 
 * @author Administrator
 * 
 */
public class CheckMemberCustomRule extends AbstractRule {

	@Resource(name = "customMerchantDao")
	private CustomMerchantDao customMerchantDao;

	@Override
	protected boolean makeDecision(Object obj) throws Exception {
		CashOrderRequest c = (CashOrderRequest) obj;
		// 查询商户定制的情况
		List<CustomMerchant> cmList = null;
		// if (DirectFlag.DIRECT.getValue() == c.getDirectFlag()) { //
		// 暂时直连不做数据库检验
		// 以后添加
		cmList = customMerchantDao.findByMembercode(c.getMemberCode());
		// }
		// else {
		// cmList = customMerchantDao.findByMembercodeAndBosstype(
		// c.getMemberCode(), c.getBossType());
		// }
		if (cmList == null || cmList.isEmpty()) {
			c.setMsgId(ResponseErrorCode.MEMBERCODE_IS_ONT_START.getValue());
			return false;
		}
		c.setCustomMerchant(cmList);
		c.setMemberName(cmList.get(0).getMerchantName());
		c.setAmountList(SelectOptionUtil.setAmountList(cmList.get(0)
				.getSupportAmountList(), c.getOrderAmount()));
		setHint(cmList, c);
		return true;
	}

	/**
	 * 设置页面提示信息
	 * 
	 * @return
	 */
	private void setHint(List<CustomMerchant> cmList, CashOrderRequest c) {
		StringBuilder pageHint = new StringBuilder();
		StringBuilder bossTypeName = new StringBuilder();
		for (CustomMerchant m : cmList) {
			pageHint.append("<li>")
					.append(m.getDescripTion() == null ? "" : m
							.getDescripTion()).append("</li>");
			// 写成静态map存储
			bossTypeName.append("<li>")
					.append(BossTypeConverUtil.getBossName(m.getBossType()))
					.append("</li>");
		}
		c.setSuBossName(bossTypeName.toString());
		c.setPageHint(pageHint.toString());
	}
}
